using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class NguoiDungDAO
    {
        public static bool Insert(NguoiDungDTO nd)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@DiaChi",nd.DiaChi));
                sqlparam.Add(new SqlParameter("@DienThoai",nd.DienThoai));
                sqlparam.Add(new SqlParameter("@HoTen",nd.HoTen));
                sqlparam.Add(new SqlParameter("@MaNguoiDung",nd.MaNguoiDung));
                sqlparam.Add(new SqlParameter("@MaQuyen",nd.MaQuyen));
                sqlparam.Add(new SqlParameter("@MatKhau",nd.MatKhau));
                sqlparam.Add(new SqlParameter("@TenDangNhap",nd.TenDangNhap));
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spNguoiDungInsert",sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Delete(NguoiDungDTO nd)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
               
                sqlparam.Add(new SqlParameter("@MaNguoiDung", nd.MaNguoiDung));
                sqlparam.Add(new SqlParameter("@TenDangNhap", nd.TenDangNhap));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("spNguoiDungDelete", sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Update(NguoiDungDTO nd)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlparam = new List<SqlParameter>();
                sqlparam.Add(new SqlParameter("@DiaChi", nd.DiaChi));
                sqlparam.Add(new SqlParameter("@DienThoai", nd.DienThoai));
                sqlparam.Add(new SqlParameter("@HoTen", nd.HoTen));
                sqlparam.Add(new SqlParameter("@MaNguoiDung", nd.MaNguoiDung));
                sqlparam.Add(new SqlParameter("@MaQuyen", nd.MaQuyen));
                sqlparam.Add(new SqlParameter("@MatKhau", nd.MatKhau));
                sqlparam.Add(new SqlParameter("@TenDangNhap", nd.TenDangNhap));
                int n = SqlDataAccessHelper.ExecuteNoneQuery("spNguoiDungUpdate", sqlparam);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static List<NguoiDungDTO> Select()
        {
            List<NguoiDungDTO> lst = new List<NguoiDungDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spNguoiDungSelect");
                foreach (DataRow dr in dt.Rows)
                {
                    NguoiDungDTO nd = new NguoiDungDTO();
                    nd.DiaChi = (dr["DiaChi"].ToString());
                    nd.DienThoai = (dr["DienThoai"].ToString());
                    nd.HoTen = (dr["HoTen"].ToString());
                    nd.MaNguoiDung = int.Parse(dr["MaNguoiDung"].ToString());
                    nd.MaQuyen = int.Parse(dr["MaQuyen"].ToString());
                    nd.MatKhau = (dr["MatKhau"].ToString());
                    nd.TenDangNhap = (dr["TenDangNhap"].ToString());
                    lst.Add(nd);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return lst;
        }
        public static NguoiDungDTO SelectByID(int MaNguoiDung)
        {
            NguoiDungDTO nd = new NguoiDungDTO();
            try
            {
                List<SqlParameter> sqlpqram = new List<SqlParameter>();
                sqlpqram.Add(new SqlParameter("@MaNguoiDung", MaNguoiDung));
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spNguoiDungSelectByID",sqlpqram);
                DataRow dr = dt.Rows[0];

                nd.DiaChi = (dr["DiaChi"].ToString());
                nd.DienThoai = (dr["DienThoai"].ToString());
                nd.HoTen = (dr["HoTen"].ToString());
                nd.MaNguoiDung = int.Parse(dr["MaNguoiDung"].ToString());
                nd.MaQuyen = int.Parse(dr["MaQuyen"].ToString());
                nd.MatKhau = (dr["MatKhau"].ToString());
                nd.TenDangNhap = (dr["TenDangNhap"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return nd;
        }
        public static NguoiDungDTO SelectByTenNguoiDung(string TenDangNhap)
        {
            NguoiDungDTO nd = new NguoiDungDTO();
            try
            {
                List<SqlParameter> sqlpqram = new List<SqlParameter>();
                sqlpqram.Add(new SqlParameter("@TenDangNhap", TenDangNhap));
                
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spNguoiDungSelectByTenNguoiDung", sqlpqram);
                DataRow dr = dt.Rows[0];

                nd.DiaChi = (dr["DiaChi"].ToString());
                nd.DienThoai = (dr["DienThoai"].ToString());
                nd.HoTen = (dr["HoTen"].ToString());
                nd.MaNguoiDung = int.Parse(dr["MaNguoiDung"].ToString());
                nd.MaQuyen = int.Parse(dr["MaQuyen"].ToString());
                nd.MatKhau = (dr["MatKhau"].ToString());
                nd.TenDangNhap = (dr["TenDangNhap"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return nd;
        }

        
    }
}
